#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
MySQL MCP Server 版本信息

这个文件定义了项目的版本号和相关元数据。
版本号遵循语义化版本控制 (Semantic Versioning) 规范。
"""

# 版本信息
__version__ = "0.1.0"
__version_info__ = (0, 1, 0)

# 项目元数据
__title__ = "MySQL MCP Server"
__description__ = "高性能的 MySQL 数据库 MCP 协议服务器"
__author__ = "Your Name"
__author_email__ = "your.email@example.com"
__license__ = "MIT"
__copyright__ = "Copyright 2024 Your Name"

# 构建信息
__build__ = "stable"
__status__ = "Beta"

# API 版本
__api_version__ = "1.0"
__protocol_version__ = "1.0"

# 兼容性信息
__python_requires__ = ">=3.9"
__mysql_min_version__ = "5.7"
__mysql_recommended_version__ = "8.0"

# 版本历史和更新日志
__changelog__ = {
    "0.1.0": {
        "date": "2024-01-01",
        "changes": [
            "初始版本发布",
            "实现基础 MCP 协议支持",
            "添加 MySQL 连接池管理",
            "实现 SQL 查询执行引擎",
            "添加日志记录功能",
            "实现配置管理模块",
            "添加错误处理机制",
            "完整的单元测试和集成测试",
        ],
        "breaking_changes": [],
        "deprecations": [],
    }
}

# 功能特性标志
__features__ = {
    "async_support": True,
    "connection_pooling": True,
    "query_caching": False,  # 计划在 0.2.0 版本中添加
    "ssl_support": True,
    "transaction_support": True,
    "prepared_statements": True,
    "batch_operations": True,
    "monitoring": True,
    "health_checks": True,
}

# 性能指标
__performance__ = {
    "max_connections": 100,
    "default_pool_size": 10,
    "query_timeout": 30,  # 秒
    "connection_timeout": 10,  # 秒
    "max_query_size": 1024 * 1024,  # 1MB
}

# 安全配置
__security__ = {
    "sql_injection_protection": True,
    "query_validation": True,
    "connection_encryption": True,
    "audit_logging": True,
}

def get_version():
    """获取版本号字符串"""
    return __version__

def get_version_info():
    """获取版本信息元组"""
    return __version_info__

def get_full_version():
    """获取完整版本信息"""
    return f"{__title__} v{__version__} ({__build__})"

def get_user_agent():
    """获取用户代理字符串"""
    return f"{__title__}/{__version__} Python"

def is_stable():
    """检查是否为稳定版本"""
    return __build__ == "stable"

def is_development():
    """检查是否为开发版本"""
    return __build__ in ["dev", "alpha", "beta", "rc"]

def get_supported_features():
    """获取支持的功能列表"""
    return [feature for feature, enabled in __features__.items() if enabled]

def check_compatibility(python_version=None, mysql_version=None):
    """检查版本兼容性"""
    import sys
    
    compatibility = {
        "python": True,
        "mysql": True,
        "warnings": [],
        "errors": []
    }
    
    # 检查 Python 版本
    if python_version is None:
        python_version = sys.version_info
    
    if python_version < (3, 9):
        compatibility["python"] = False
        compatibility["errors"].append(
            f"Python {python_version} 不受支持，需要 {__python_requires__}"
        )
    elif python_version < (3, 10):
        compatibility["warnings"].append(
            "建议使用 Python 3.10 或更高版本以获得更好的性能"
        )
    
    # 检查 MySQL 版本（如果提供）
    if mysql_version:
        try:
            mysql_ver = tuple(map(int, mysql_version.split('.')))
            min_ver = tuple(map(int, __mysql_min_version__.split('.')))
            rec_ver = tuple(map(int, __mysql_recommended_version__.split('.')))
            
            if mysql_ver < min_ver:
                compatibility["mysql"] = False
                compatibility["errors"].append(
                    f"MySQL {mysql_version} 不受支持，需要 {__mysql_min_version__} 或更高版本"
                )
            elif mysql_ver < rec_ver:
                compatibility["warnings"].append(
                    f"建议使用 MySQL {__mysql_recommended_version__} 或更高版本"
                )
        except ValueError:
            compatibility["warnings"].append(
                f"无法解析 MySQL 版本号: {mysql_version}"
            )
    
    return compatibility

# 版本比较函数
def compare_version(other_version):
    """比较版本号"""
    try:
        other_parts = tuple(map(int, other_version.split('.')))
        if __version_info__ > other_parts:
            return 1
        elif __version_info__ < other_parts:
            return -1
        else:
            return 0
    except ValueError:
        raise ValueError(f"无效的版本号格式: {other_version}")

if __name__ == "__main__":
    print(get_full_version())
    print(f"API 版本: {__api_version__}")
    print(f"协议版本: {__protocol_version__}")
    print(f"Python 要求: {__python_requires__}")
    print(f"支持的功能: {', '.join(get_supported_features())}")
    
    # 检查当前环境兼容性
    compat = check_compatibility()
    if compat["errors"]:
        print("\n错误:")
        for error in compat["errors"]:
            print(f"  - {error}")
    if compat["warnings"]:
        print("\n警告:")
        for warning in compat["warnings"]:
            print(f"  - {warning}")